<template>
  <div>
    <el-card>
      <div style="display: flex; justify-content: space-between;">
        <el-statistic title="会员总数" :value="totalMembers" />
        <el-input v-model="searchQuery" placeholder="搜索会员..." style="width: 300px;" />
      </div>
    </el-card>
    <!-- 表格数据来源是后端查询的数据 -->
    <el-table :data="Members" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="memberNum" label="会员号" />
      <el-table-column prop="memberName" label="姓名" />
      <el-table-column prop="memberSex" label="性别">
      </el-table-column>
      <el-table-column prop="phoneNum" label="电话" />
      <el-table-column prop="card" label="健身卡" />
      <el-table-column prop="huiji" label="会籍顾问" />
      <el-table-column label="操作">
        <!--  -->
        <template #default="scope">
          <!-- <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button> -->
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑对话框 -->
    <!-- <el-dialog v-model="editDialogVisible" title="编辑会员"> -->
      
      <!-- 编辑表单 -->
        <!-- <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules"
      label-width="auto">
       <el-form-item label="姓名" prop="memberName"> 
    <el-input v-model="memberInfo" />
  </el-form-item>
  
  <el-form-item label="性别" prop="memberSex"> 
    <el-radio-group v-model="memberInfo">
      <el-radio value="true">男</el-radio>
      <el-radio value="false">女</el-radio>
    </el-radio-group>
  </el-form-item>
  
  <el-form-item label="电话号码" prop="phoneNum"> 
    <el-input v-model="memberInfo" />
  </el-form-item>
     <el-form-item label="健身卡" prop="card">
    <el-select v-model="ruleForm.card">
      <el-option 
        v-for="item in cardOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
  </el-form-item>
  
  <el-form-item label="会籍顾问" prop="huiji">
    <el-select v-model="ruleForm.huiji">
      <el-option 
        v-for="item in huijiOptions"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </el-select>
  </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          确定
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">
          清空
        </el-button>
      </el-form-item>
    </el-form>
    </el-dialog> -->
  </div>
</template>

<script setup>
import { ref, computed, onMounted} from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'


// 定义响应式数组数据members
const members = ref([])
// 搜索会员响应数据
const searchQuery = ref('')

// 总会员数量 计算数组长度
const totalMembers = computed(() => members.value.length)

//过滤数据表 将需要的输出 
const Members = computed(() => {
    // 如果搜索的关键字的值为空 则返回所以数组值
  if (!searchQuery.value) return members.value
// 否则返回搜索的  
  return members.value.filter(member => 
  //  将单个会员的姓名和号码作为关键字 通过姓名和号码来过滤筛选
    member.memberName.includes(searchQuery.value) || 
    member.phoneNum.includes(searchQuery.value))
})
// 挂载后端请求的数据
onMounted(async () => {
    // 获取member表 /api/members是后端的相对地址
  const response = await axios.get('/api/members')
  members.value = response.data
})

// 编辑框是否可见 默认不可见
// const editDialogVisible = ref(false)
// const handleEdit = async(member) => {
//   const memberInfo= await axios.get(`/api/members/${member.memberNum}`)
//   // 打开编辑对话框
//   editDialogVisible.value=true
//   member.value =memberInfo.data
//   console.log(member.value)
// }

const handleDelete = async (member) => {
  await axios.delete(`/api/members/${member.memberNum}`)
  members.value = members.value.filter(m => m.meberNum !== member.meberNum)
  ElMessage.success('删除成功!')
  // 再次渲染删除后的列表
  const update = await axios.get('/api/members')
  members.value = update.data


  onMounted(() => {
  handleDelete()
})
}

</script>